//used
//id:string
//description: string
//genetic:string
//priortiy;num
//isActive:boolean
package holdem.Rulegui;

import holdem.Emotion;
import holdem.ListInt;

import java.io.Serializable;
import java.util.Scanner;
import java.util.Vector;


public class Rule implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4878869141839776367L;
	private static String prefix[] = { "id:" , "des:" , "pri:" , "act:", "preCond:" , "actions:"};
	private Expression preCondeition;
	private Vector<ActionExpresion> listOfActions;
	private int Id;
	private Emotion pri;
	private String Description;
	private boolean active;
	private int numberCalled;
	
	public static Rule load(String s){
		Rule Rval = new Rule();
		String[] ruleArray = s.split("\n");
		int j = 0;
		for(int i = 0; i< ruleArray.length; i++){
			if(s.contains(prefix[j])){
				j++;
				switch(j){
					case 0://id
						Rval.Id =  new Scanner(ruleArray[i].split(":")[1]).nextInt();
						break;
					case 1:
						Rval.Description = ruleArray[i].split(":")[1];
						break;
					case 2:
						Rval.pri = parseEmotion(ruleArray[i].split(":")[1]);
						break;
					case 3:
						Rval.active = parseActive(ruleArray[i].split(":")[1]);
						break;
					case 4:
						Rval.preCondeition = Expression.load(ruleArray[i].split(":")[1]);
						break;
					case 5:
						Rval.listOfActions = parseAction(ruleArray[i].split(":")[1]);
						break;
				}	
			}
			if(j == prefix.length){//got what i need cut and run
				break;
			}
		}
		return Rval;
	}
	
	
	public String saveString(){
		// { "id:" , "des:" , "pri:" , "act:", "preCond:" , "actions:"};
		String Rval="";
		int i  =0;
		Rval = prefix[i++] + Id + '\n';
		Rval = prefix[i++] + Description + '\n';
		Rval = prefix[i++] + saveStringPri() + '\n';
		Rval = prefix[i++] + saveStringAct() + '\n';
		Rval = prefix[i++] + preCondeition.saveString() + '\n';
		Rval = prefix[i++] + saveStringAction() + '\n';
		return Rval;
	}
	
	private String saveStringAction(){
		String Rval= "";
		for(int i= 0; i < listOfActions.size(); i++){
			Rval += listOfActions.get(i).saveString() +";";
		}
		return Rval;
	}
	private String saveStringAct(){
		if(active){ return 1 +"";}
		return 0 +"";
		
	}
	private String saveStringPri(){
		String s ="";
		s = pri.getAgression() +" ";
		s += pri.getAssertiveness() +" ";
		s += pri.getLogical()+" ";
		s += pri.getChaotic() +" ";
		s += pri.getConfidence() +" ";
		return s;
	}
	public int NumberCAlled(){
		return numberCalled;
	}
	
	public void setPrecondition(Expression precond){
		preCondeition = precond;
	}
	public Expression getPrecondition(){
		return preCondeition;
	}
	public Vector<ActionExpresion> getActionlist(){
		return listOfActions;
	}
	public void setActionList(Vector<ActionExpresion> listofaction){
		listOfActions = listofaction;
	}
	public void removeAction(int i){
		listOfActions.remove(i);
	}
	public void addAction(ActionExpresion action ){
		listOfActions.add(action);
	}
	public void setEmotion(Emotion emotepri){
		pri = emotepri;
	}
		
	public Emotion getPri(){
		return this.pri;
	}
	
	public void enable(boolean active){
		this.active = active;
	}
	
	
	public String getDescrition(){
		return Description;
	}
	public void setDecription(String s){
		Description = s;
	}
	
	public boolean preondition( GeneticCode code, ListInt WM  ){
		 return this.preCondeition.EvalExp(code, WM, code) != 0;
	}
	public void exicute(GeneticCode code , ListInt WM , ListInt modifyRelm){
		numberCalled++;
		for(int i = 0; i < listOfActions.size(); i++){
			listOfActions.get(i).Action(code, WM,modifyRelm);
		}
	}
	
	
	
	public static boolean parseActive(String S){
		Scanner scan = new Scanner(S);
		//next int = 0
		return scan.nextInt() != 0; 
	}
	
	public static Emotion parseEmotion(String s){
		int[] emote = new int[5];
		Scanner scan = new Scanner(s);
		for(int i = 0; i < emote.length; i++){
			emote[i] = scan.nextInt();
		}
		return new Emotion(emote[0],emote[1],emote[2],emote[3],emote[04]);
		
	}
	public static boolean canParse(String s){
		boolean b = true;
		for(int i = 0 ; i < prefix.length; i++){
			b = b && s.contains(prefix[i]);
		}
		return b;
	}
	
	private static Vector<ActionExpresion> parseAction(String s){
		String[] actions = s.split(";");
		Vector<ActionExpresion> list = new Vector<ActionExpresion>();
		for(int i = 0;  i < actions.length -1; i++){//beacuse this is a terminalor symbol there will be one more item int the array then there needs to be
			list.add( ActionExpresion.Load(actions[i]));
		}
		return list; 
	}
	
	
}
